package com.xingmei.springbootdemo.controller;

import com.xingmei.springbootdemo.common.ResultVo;
import com.xingmei.springbootdemo.model.User;
import com.xingmei.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/get-by-id")
    public ResultVo<User> getUser(@RequestParam(name = "id") Long id) {
        return ResultVo.success(userService.getById(id));
    }

    @GetMapping(value = "/save")
    public ResultVo<?> saveUser(@RequestParam(name = "age") Integer age, @RequestParam(name = "name") String name) {
        return ResultVo.success(userService.save(User.builder().name(name).age(age).build()));
    }

    @GetMapping(value = "/get-by-name")
    public ResultVo<List<User>> getUserByName(@RequestParam(name = "name") String name) {
        return ResultVo.success(userService.selectByName(name));
    }
}
